package com.ek.kademlia;

import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;

import com.ek.common.Bucket;
import com.ek.common.Observable;

public class ObservableBucket implements Bucket, Observable {

    private static final long serialVersionUID = 8971693432158393246L;
    private Bucket bucket = null;
    private boolean seen = false;

    public ObservableBucket(Bucket bucket) {
        this.bucket = bucket;
    }

    public String getAddress() {
        return bucket.getAddress();
    }

    public byte[] getNodeID() {
        return bucket.getNodeID();
    }

    public int getPort() {
        return bucket.getPort();
    }

    public boolean alreadySeen() {
        return seen;
    }

    public void setAlreadySeen(boolean seen) {
        this.seen = seen;
    }
    
    @Override
    public String toString() {
        StringBuffer builder = new StringBuffer(64);
        builder.append("ObservableBucket{\n");
        builder.append("  address:" + getAddress() + "\n");
        builder.append("  port:" + getPort() + "\n");
        builder.append("  nodeID:" + new String(getNodeID()) + "\n");
        builder.append("  alreadySeen:" + alreadySeen() + "\n");
        builder.append("}\n");
        return builder.toString();
    }

    @Override
    public int hashCode() {
        HashCodeBuilder builder = new HashCodeBuilder();
        builder.append(bucket);
        return builder.toHashCode();
    }
    
    @Override
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }
        if (obj == this) {
            return true;
        }
        if (obj.getClass() != getClass()) {
            return false;
        }
        ObservableBucket rhs = (ObservableBucket) obj;
        return new EqualsBuilder()
                      .append(bucket.getAddress(), rhs.getAddress())
                      .append(bucket.getNodeID(), rhs.getNodeID())
                      .append(bucket.getPort(), rhs.getPort())
                      .isEquals();

    }

}
